package com.whsxt.controller;

import com.github.pagehelper.PageInfo;
import com.whsxt.common.AjaxResult;
import com.whsxt.domain.Rent;
import com.whsxt.dto.RentDto;
import com.whsxt.service.RentService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Arrays;

@RestController
@RequestMapping("/rent")
@RequiresRoles(value={"admin","business", "user","devops"}, logical = Logical.OR)
public class RentController {

    @Autowired
    private RentService rentService;

    /**
     * 查询分页的租车记录数据
     * @param rentDto
     * @return
     */
    @RequestMapping("/findByPage.do")
    public AjaxResult findByPage(RentDto rentDto){
        PageInfo<Rent> rentPageInfo = rentService.select(rentDto);
        return AjaxResult.ok(rentPageInfo);
    }

    /**
     * 删除租车数据接口
     * @param id
     * @return
     */
    @RequestMapping("/delete.do")
    @RequiresPermissions("rent:delete")
    public AjaxResult delete(Integer[] id){
        boolean isOk = rentService.batchDelete(Arrays.asList(id));
        return isOk ? AjaxResult.ok() : AjaxResult.error("删除数据失败");
    }

    /**
     * 清空租车记录的接口
     * @return
     */
    @RequestMapping("/clear.do")
    @RequiresPermissions("rent:delete")
    public AjaxResult clear(){
        boolean isOk = rentService.clear();
        return isOk ? AjaxResult.ok() : AjaxResult.error("清空数据失败");
    }

    /**
     * 计算租赁的支付价格接口
     * @return
     */
    @RequestMapping("/calcPayMoney.do")
    public AjaxResult calcPayMoney(Integer id){
        BigDecimal pay = rentService.getPayMoney(id);
        return AjaxResult.ok(pay);
    }

    /**
     * 还车的接口
     * @param id 租赁记录的id
     * @return
     */
    @RequestMapping("/doReturn.do")
    @RequiresPermissions("rent:car_return")
    public AjaxResult doReturn(Integer id){

        boolean isOk = rentService.returnCar(id);
        return isOk?AjaxResult.ok() : AjaxResult.error("还车失败") ;
    }
}
